package cn.lingyangwl.agile.infra.weixin.mp.controller;

import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.framework.validation.group.UpdateGroup;
import cn.lingyangwl.agile.infra.weixin.mp.model.rqrs.MpMaterialPageReq;
import cn.lingyangwl.agile.infra.weixin.mp.model.rqrs.MpMaterialResp;
import cn.lingyangwl.agile.infra.weixin.mp.model.rqrs.MpMaterialSaveReq;
import cn.lingyangwl.agile.infra.weixin.mp.model.rqrs.MpMaterialUpdateReq;
import cn.lingyangwl.agile.infra.weixin.mp.service.MpMaterialService;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 公众号素材
 * 
 * @author shenguangyang
 */
@Api(tags = "公众号素材")
@RestController
@RequestMapping("/mpMaterial")
public class MpMaterialController {
    @Resource
    private MpMaterialService mpMaterialService;

    @ApiOperation(value = "分页-公众号素材")
    @RamResource(action = "weixin:mpMaterial:query", name = "查询公众号素材列表", type = ActionType.READ)
    @GetMapping("/pageMpMaterial")
    public Resp<IPage<MpMaterialResp>> pageMaterial(@Validated MpMaterialPageReq query) {
        return Resp.ok(mpMaterialService.pageMaterial(query));
    }
  

    @ApiOperation(value = "通过id查找-公众号素材")
    @RamResource(action = "weixin:mpMaterial:query", name = "查询公众号素材详情", type = ActionType.READ)
    @GetMapping("/getMpMaterial")
    public Resp<MpMaterialResp> getMpMaterialById(@RequestParam(value = "id") Long id) {
        return Resp.ok(mpMaterialService.getMaterialById(id));
    }

    @ApiOperation(value = "保存-公众号素材")
    @RamResource(action = "weixin:mpMaterial:save", name = "保存公众号素材", type = ActionType.WRITE)
    @PostMapping("/savePermanentMaterial")
    public Resp<?> saveMpMaterial(@Validated @RequestBody MpMaterialSaveReq rq) throws Exception {
        return Resp.ok(mpMaterialService.savePermanentMaterial(rq));
    }

    @ApiOperation(value = "更新-公众号素材")
    @RamResource(action = "weixin:mpMaterial:update", name = "更新公众号素材", type = ActionType.WRITE)
    @PostMapping("/updateMpMaterial")
    public Resp<?> updateMpMaterialById(@Validated(UpdateGroup.class) @RequestBody MpMaterialUpdateReq rq) {
        return Resp.ok(mpMaterialService.updateMaterialById(rq));
    }


    @ApiOperation(value = "删除-公众号素材")
    @RamResource(action = "weixin:mpMaterial:delete", name = "删除公众号素材", type = ActionType.WRITE)
    @DeleteMapping("/deleteMpMaterial")
    public Resp<?> delete(@RequestParam(value = "id") Long id) {
        return Resp.ok(mpMaterialService.deleteMaterialById(id));
    }
}
